package com.githan.bootsecurity.config;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author https://github.com/Han56
 * @date 2021/4/11 16:48
 * JSON数据封装类
 */
public abstract class JSONAuthentication {

    protected void WriteJSON(HttpServletRequest httpServletRequest,
                             HttpServletResponse httpServletResponse,
                             Object data
    )throws IOException, ServletException{
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Method", "POST,GET");
        //输出JSON
        PrintWriter out = httpServletResponse.getWriter();
        String returnMessage = JSON.toJSONString(data);
        out.write(returnMessage);
        out.flush();
        out.close();
    }

}
